package org.cmroot.certmaster.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import org.cmroot.certmaster.client.widgets.ErrorLabel;
import org.cmroot.certmaster.client.widgets.OpensslText;
import org.cmroot.certmaster.client.widgets.PKITree;
import org.cmroot.certmaster.shared.dto.X509DTO;

import java.util.List;

/**
 * Copyright (c) 2010 Andrey Domas <andrey.domas at gmail.com>.
 * *
 * This file is part of certmaster.
 * *
 * Certmaster is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * *
 * Certmaster is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see http://www.gnu.org/licenses
 * or write to the Free Software Foundation,Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301  USA
 */

public class CertsScreen extends BaseScreen {
    interface AdminScreenUiBinder extends UiBinder<Widget, CertsScreen> {
    }

    private static AdminScreenUiBinder ourUiBinder = GWT.create(AdminScreenUiBinder.class);


    @UiField
    PushButton button_import_certificate_chain;
    @UiField
    PushButton button_cancel_import_certificate_chain;
    @UiField
    PushButton button_submit_import_certificate_chain;
    @UiField
    VerticalPanel import_ceritificate_chain_dialog;
    @UiField
    TextArea import_certificate_chain_text;
    @UiField
    ErrorLabel errorLabel;
    @UiField
    SimplePanel pkiTree_panel;
    @UiField
    SimplePanel opensslText_panel;
    PKITree pkiTree;
    OpensslText opensslText;

    public CertsScreen() {
        initWidget(ourUiBinder.createAndBindUi(this));

        pkiTree = new PKITree(errorLabel, new SelectionHandler<TreeItem>() {
            @Override
            public void onSelection(SelectionEvent<TreeItem> treeItemSelectionEvent) {
                opensslText.showCert(((X509DTO) treeItemSelectionEvent.getSelectedItem().getUserObject()).getId());
            }
        });

        opensslText = new OpensslText(errorLabel);
        opensslText_panel.add(opensslText);

        pkiTree_panel.add(pkiTree);
        pkiTree.refreshTree();

        button_import_certificate_chain.addClickHandler( new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                errorLabel.resetError();
                button_import_certificate_chain.setVisible(false);
                import_ceritificate_chain_dialog.setVisible(true);
            }
        });

        button_cancel_import_certificate_chain.addClickHandler( new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                import_ceritificate_chain_dialog.setVisible(false);
                import_certificate_chain_text.setText(null);
                button_import_certificate_chain.setVisible(true);
            }
        });

        button_submit_import_certificate_chain.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent clickEvent) {
                import_ceritificate_chain_dialog.setVisible(false);
                GWTClientService.App.getInstance().importChain(import_certificate_chain_text.getText(),
                        new AsyncCallback<List<X509DTO>>() {
                    @Override
                    public void onFailure(Throwable throwable) {
                        errorLabel.showError(throwable.getMessage());
                    }

                    @Override
                    public void onSuccess(List<X509DTO> x509DTOs) {
                        pkiTree.refreshTree();
                    }
                });
                import_certificate_chain_text.setText(null);
                button_import_certificate_chain.setVisible(true);
            }
        });

    }
}